package TemplateTest;
use strict;
use warnings;

use base qw(Test::Class);
use Test::More;
use Data::Dumper;

use lib '/home/'.$ENV{USER}.'/djpp/core/service';
use lib '/home/'.$ENV{USER}.'/djpp/core/model';
use Template; 
use Context;
use Time::HiRes qw(gettimeofday);

sub testSetup : Test(setup)
{
	my $self = shift;
	
	$self->{tmpl} = new Template( path => ['/home/'.$ENV{USER}.'/djpp/core/t/service/templates'] );
}

sub testDefaultCreate : Test
{
	my $self = shift;
	
	ok($self->{tmpl}, "Create OK");
	for my $dir (@{ $self->{tmpl}->path }){
		ok(-e $dir, "Template directory exists")
	}
}

sub testGetTemplate : Test
{
	my $self = shift;
	
	my $tmpl = $self->{tmpl}->get("test.tmpl");
	
	ok($tmpl, "Template loaded");
}

sub testGetHtml : Test
{
	my $self = shift;

	my $t0 = gettimeofday;

	my $tmpl = $self->{tmpl}->get("test.tmpl");
	my $html = $tmpl->render( new Context ( testvar => "testval") );

	my $delta = gettimeofday - $t0;
	
	ok($html, "Render OK");	
	ok($html =~ /testval/ms, "Value is finded");
	ok($delta < 0.001, "Template work is OK: more then 0.001: ".$delta);
}

1;